package com.example.design.ddd.practice2.domain.shipping.entity;

import cn.hutool.core.lang.ObjectId;
import com.example.design.ddd.practice2.domain.shipping.ShippingStatus;

import java.time.LocalDateTime;

/**
 * 物流聚合
 */
public class Shipping {
    private String shippingId;
    private String orderId;
    private String trackingNumber;
    private ShippingStatus status;
    private LocalDateTime shippedAt;

    public static Shipping create(String orderId) {
        Shipping shipping = new Shipping();
        shipping.shippingId = ObjectId.next();
        shipping.orderId = orderId;
        shipping.status = ShippingStatus.PENDING;
        return shipping;
    }

    public void ship(String trackingNumber) {
        if (status != ShippingStatus.PENDING) {
            throw new IllegalStateException("物流单状态异常");
        }
        this.trackingNumber = trackingNumber;
        this.status = ShippingStatus.SHIPPED;
        this.shippedAt = LocalDateTime.now();
    }
}